<?php

class StockController extends Base_Controller
{

   public function init()
    {
    	parent::init();
    }

    public function showallstockinAction() {
    	$db = Zend_Db_Table::getDefaultAdapter();
    	$sql = 'select a.*, b.name entrepot_name from stockin a, entrepot b where a.entrepot_id = b.id order by a.create_date;';
    	$this->view->stockin = $db->fetchAll($sql);
    	
    }
    public function showonestockinAction() {
      	$stockinId = $this->getRequest()->getParam('stockinId');
    	$this->logger->info("表示入库单： $stockinId");
    	$stockinMapper = new Application_Model_Dao_StockinMapper();
    	$this->view->stockin = $stockinMapper->getStockinById($stockinId);
    	$this->logger ->debug(print_r($this->view->stockin, true));
     	
    }
    public function deleteAction() {
      	$stockinId = $this->getRequest()->getParam('stockinId');
    	$this->logger->info("删除入库单： $stockinId");
    	$db = Zend_Db_Table::getDefaultAdapter();
    	$db->beginTransaction();
    	try {
	    	$stockMapper = new Application_Model_Dao_StockMapper();
	    	$stockMapper -> deleteByStockinId($stockinId);
	    	$stockinMapper = new Application_Model_Dao_StockinMapper();
	    	$stockinMapper->delete($stockinId);
	    	$db->commit();
    		$this->logger->info("删除入库单： $stockinId 成功");
    	} catch (Exception $e) {
    		$this->logger->info("删除入库单： $stockinId 失败");
    		$this->logger->error($e->getMessage());
    		$db->rollBack();
    	}
    	
    	return $this->_helper->redirector('showallstockin');
    	     	
    }
    
    
    
    public function stockinAction()
    {
    	
        $emapper = new Application_Model_Dao_EntrepotMapper();
        $this->view->entrepots = $emapper ->fetchAll();
        
        $steel = new Application_Model_Dao_SteelMapper();
        $this->view->steels = $steel->fetchAll();
        
        $dmapper = new Application_Model_Dao_DensityMapper();
        $this->view->specs = $dmapper->fetchAll();
        
        if($this->getRequest()->isPost()) {
        	$this->logger->info("创建入库单");
        	$db = Zend_Db_Table::getDefaultAdapter();
        	$db->beginTransaction();
        	try {
	        	$stockinMapper = new Application_Model_Dao_StockinMapper();
	        	$tockinModel = new Application_Model_BusiInfo_Stockin();
	        	$tockinModel->set_id($_POST['stockinId']);
	        	$tockinModel->__set("entrepotId",$_POST['entrepotId']);
	        	$tockinModel->__set("position",$_POST['position']);
	        	$stockinId = $stockinMapper->save($tockinModel);
	        	
	        	$stockId = $_POST['stockId'];
	        	$steelIds = $_POST['steel'];
	        	$specs = $_POST['specs'];
	        	$length = $_POST['length'];
	        	$amount = $_POST['amount'];
	        	$weight = $_POST['weight'];
	        	$costPrice = $_POST['costPrice'];
	        	
	        	$stockMapper = new Application_Model_Dao_StockMapper();
	        	
	        	foreach ($steelIds as $key=>$value) {
	        		$stockModel = new Application_Model_BusiInfo_Stock();
	        		$stockModel->setOptions(array(
	        			"id" => $stockId[$key],
	        			"stockinId" => $stockinId,
	        			"steelId" => $steelIds[$key],
	        			"specs" => $specs[$key],
	        			"length" => $length[$key],
		        		"amount" => $amount[$key],
		        		"weight" => $weight[$key],
		        		"costPrice" => $costPrice[$key],
	        		));
	        		$stockMapper->save($stockModel);
	        		
	        	}
	        	$db->commit();
	        	$this->logger->info("创建入库单 $stockinId 成功");
        	}catch (Exception $e) {
        		$db->rollBack();
        		$this->logger->info("创建入库单失败");
        	}
        	return $this->_helper->redirector('showallstockin');
        }
        
    }


}

